package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.ReadLogConverter;
import com.dycx.user.dto.ReadLogDTO;
import com.dycx.user.entity.ReadLogDO;
import com.dycx.user.service.ReadLogService;
import com.dycx.user.vo.ReadLogVO;
import com.dycx.user.mapper.ReadLogMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户阅读日志(ReadLog)表服务实现类
 *
 * @author wuzongbing
 * @since 2024-08-24 17:56:31
 */

@Service
@AllArgsConstructor
public class ReadLogImpl extends ServiceImpl<ReadLogMapper, ReadLogDO> implements ReadLogService {

    private ReadLogMapper readLogMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<ReadLogVO> getPage(ReadLogDTO dto) {
        LambdaQueryWrapper<ReadLogDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getId()), ReadLogDO::getId, dto.getId());
        wrapper.orderByDesc(ReadLogDO::getId);

        Page<ReadLogDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);
        Page<ReadLogVO> voPage = ReadLogConverter.entityToVoPage(entityPage);
        voPage.setRecords(setListData(voPage.getRecords(), dto));

        return voPage;
    }


    /**
     * 设置数据
     *
     * @param voList 实体
     * @return 查询结果
     */
    public List<ReadLogVO> setListData(List<ReadLogVO> voList, ReadLogDTO dto) {
        return voList;
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public ReadLogVO load(Integer id) {
        ReadLogDO entity = getById(id);

        return ReadLogConverter.entityToVo(entity);
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(ReadLogDTO dto) {
        ReadLogDO entity = BeanUtil.toBean(dto, ReadLogDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(ReadLogDTO dto) {
        ReadLogDO entity = BeanUtil.toBean(dto, ReadLogDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return readLogMapper.getMaxSort();
    }
}

